package com.uy.cuantoconocesdeuruguay;

import java.io.UnsupportedEncodingException;

import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.util.Log;

import com.google.android.gms.gcm.GoogleCloudMessaging;
import com.uy.cuantoconocesdeuruguay.utiles.StringPool;

public class GcmIntentService extends IntentService {
	
	private enum INTENTS {
		CHALLENGES("challenges"), MAIN_MENU("main_menu"), SUGGEST_PICTURE("suggest_picture");
		String value;
		private INTENTS(String type) {
			this.value = type;
		}
	}
    private static int NOTIFICATION_ID = 1;
    private static final Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
	private static final String MESSAGE_KEY = "mensaje";

    public GcmIntentService() {
        super("GcmIntentService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Bundle extras = intent.getExtras();
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
        String messageType = gcm.getMessageType(intent);

        if (!extras.isEmpty()) {  // has effect of unparcelling Bundle
            if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
                showNotification(extras);
            } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
                showNotification(extras);
            } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
                showNotification(extras);
                Log.i("", "Received: " + extras.toString());
            }
        }
        GcmBroadcastReceiver.completeWakefulIntent(intent);
    }
 
    private synchronized void showNotification(Bundle extras) {   
    	String msg = extras.getString(MESSAGE_KEY);
    	try {
			msg = new String(msg.getBytes(), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
    	String intent = extras.getString("intent");
        String[] tokens = msg.split(StringPool.NOTIFICATION_SPLITTER); 
         
    	NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
	        .setSmallIcon(R.drawable.icon)
	        .setContentTitle(StringPool.APP_NAME)
	        .setContentText(tokens[0])
	        .setDefaults(Notification.DEFAULT_VIBRATE)
	        .setSound(alarmSound)
	        .setAutoCancel(true);
    	/* Add Big View Specific Configuration */
        NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
        
        // Sets a title for the Inbox style big view
        inboxStyle.setBigContentTitle(StringPool.APP_NAME);
        for (String token : tokens){
           inboxStyle.addLine(token);
        }
        mBuilder.setStyle(inboxStyle);
        
		Intent resultIntent = new Intent(this, MainActivity.class);
		if (intent.equals(INTENTS.MAIN_MENU.value)){
		}else if (intent.equals(INTENTS.CHALLENGES.value)){
			resultIntent.putExtra(MainActivity.FROM_CHALLENGE_NOTIFICATION, true);
		}else if (intent.equals(INTENTS.SUGGEST_PICTURE.value)){
		}else{
		}
		TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
		stackBuilder.addParentStack(MainActivity.class);
		stackBuilder.addNextIntent(resultIntent);
		PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
		mBuilder.setContentIntent(resultPendingIntent);
		NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
		mNotificationManager.notify(NOTIFICATION_ID++, mBuilder.build());
    }
}
